package com.schedule.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.schedule.entity.Schedule;
import com.schedule.service.ScheduleServiceImpl;

@Controller
public class ScheduleController {
	@Autowired
	ScheduleServiceImpl scheduleServiceImpl;
	
	@RequestMapping("/addSchedule.action")
	public void addSchedule(HttpServletRequest request, HttpServletResponse response,Schedule schedule) throws ServletException, IOException {
		/*ModelAndView modelAndView=new ModelAndView();*/
		String start_minute=request.getParameter("start_minute");
		String end_minute=request.getParameter("end_minute");
		schedule.setStart_time(schedule.getStart_time()+":"+start_minute);
		schedule.setEnd_time(schedule.getEnd_time()+":"+end_minute);
		scheduleServiceImpl.AddSchedule(schedule);
//		modelAndView.setViewName("main");
		/*return modelAndView;*/
		/*request.getRequestDispatcher("static/jsp/main.jsp").forward(request, response);*/
		response.sendRedirect("jsp/schedule.jsp?code=1&user="+schedule.getUser());
	    // �����ض���ָ��ҳ��
	}
	@ResponseBody
	@RequestMapping(value="/selectSchedule.action",produces = "text/json;charset=UTF-8")
	public String selectSchedule(HttpServletRequest request, HttpServletResponse response) {
		String param_data=request.getParameter("param");
		JsonParser jp=new JsonParser();
		JsonObject jo_data=jp.parse(param_data).getAsJsonObject();
		String user=jo_data.get("user").getAsString();
		String start_date=jo_data.get("start_date").getAsString();
		List<Schedule> list=scheduleServiceImpl.SelectSchedule(user, start_date);
		Gson gson=new Gson();
		String gson1=gson.toJson(list);
		/*System.out.println(gson1);*/
		return gson1;
	}
	@RequestMapping("/deleteSchedule.action")
	public void deleteSchedule(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Schedule schedule =scheduleServiceImpl.SelectById(id);
		scheduleServiceImpl.DeleteSchedule(id);
		response.sendRedirect("jsp/schedule.jsp?code=1&user="+schedule.getUser());
	}
	@RequestMapping("/SelectOne.action")
	public void SelectOne(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Schedule schedule =scheduleServiceImpl.SelectById(id);
		
		String start_time=schedule.getStart_time();
		String end_time=schedule.getEnd_time();
		String[] split1=start_time.split(":");
		String[] split2=end_time.split(":");
		String start_hour=split1[0];
		String start_minute=split1[1];
		String end_hour=split2[0];
		String end_minute=split2[1];
		/*request.setAttribute("start_hour",start_hour);
		request.setAttribute("start_minute",start_minute);
		request.setAttribute("end_hour",end_hour);
		request.setAttribute("end_minute",end_minute);
		request.setAttribute("schedule", schedule);*/
		HttpSession session=request.getSession();
		session.setAttribute("start_hour",start_hour);
		session.setAttribute("start_minute",start_minute);
		session.setAttribute("end_hour",end_hour);
		session.setAttribute("end_minute",end_minute);
		session.setAttribute("schedule", schedule);
		response.sendRedirect("static/jsp/edit.jsp?code=1&user="+schedule.getUser());
	}
	@RequestMapping("/EditSubmit.action")
	public void EditSubmit(HttpServletRequest request, HttpServletResponse response,Schedule schedule) throws IOException {
		String start_minute=request.getParameter("start_minute");
		String end_minute=request.getParameter("end_minute");
		schedule.setStart_time(schedule.getStart_time()+":"+start_minute);
		schedule.setEnd_time(schedule.getEnd_time()+":"+end_minute);
		//System.out.println(schedule.getId());
		scheduleServiceImpl.UpdateSchedule(schedule);
		response.sendRedirect("jsp/schedule.jsp?code=1&user="+schedule.getUser());
	}
	@ResponseBody
	@RequestMapping(value="/SelectScheduleByWarn.action",produces = "text/json;charset=UTF-8")
	public String SelectScheduleByWarn(HttpServletRequest request, HttpServletResponse response) {
		String param_data=request.getParameter("param");
		JsonParser jp=new JsonParser();
		JsonObject jo_data=jp.parse(param_data).getAsJsonObject();
		String warn=jo_data.get("warn").getAsString();
		String time=jo_data.get("start_date").getAsString();
		String user=jo_data.get("user").getAsString();
		List<Schedule> list=scheduleServiceImpl.SelectScheduleByWarn(user,warn, time);
		Gson gson=new Gson();
		String gson1=gson.toJson(list);
		return gson1;
	}
	@ResponseBody
	@RequestMapping(value="/selectAllSchedule.action",produces="text/json;charset=UTF-8")
	public String selectAllSchedule(@RequestBody String param) {
		JsonParser jp=new JsonParser();
		JsonObject jo_data=jp.parse(param).getAsJsonObject();
		String user=jo_data.get("user").getAsString();
//		System.out.println(user);
		List<Schedule> list = scheduleServiceImpl.SelectAllSchedule(user);
		/*System.out.println(list);*/
		Gson gson=new Gson();
		String gson1=gson.toJson(list);
		return gson1;
	}
	
}
